package servlet;

import Data.data;
import hbase.HbaseUtilA;
import hbase.HbaseUtilU;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet("/update")
public class Update extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        updata(request,response);
    }


    private void updata(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        String number;
        String id=request.getParameter("id");
        String name=request.getParameter("name");
        String price=request.getParameter("price");
        String time=request.getParameter("time");
        String status=request.getParameter("status");
//        ArrayList<data> dataList = new ArrayList<>();
        //  创建HBase配置对象
        Configuration configuration = HBaseConfiguration.create();
        // 指定zookeeper集群地址
        configuration.set("hbase.zookeeper.quorum", "192.168.116.11");
        // 创建连接对象
        Connection connection = ConnectionFactory.createConnection(configuration);
        // 获取Table对象,Table负责与记录相关的操作,如增删改查等
        Table table = connection.getTable(TableName.valueOf("goodsmessage"));

        Scan scan = new Scan();
        scan.setBatch(100);
        ResultScanner result = table.getScanner(scan);

        for (Result rs : result) {
            String a = null;
            String b = null;
            String c = null;
            String d = null;
            String e = null;
            String f=null;
            for (KeyValue kv : rs.raw()) {

                if (new String(kv.getQualifier()).equals("id")) {
                    a = new String(kv.getValue());
                } else if (new String(kv.getQualifier()).equals("name")) {
                    b = new String(kv.getValue());
                } else if (new String(kv.getQualifier()).equals("price")) {
                    c = new String(kv.getValue());
                } else if (new String(kv.getQualifier()).equals("time")) {
                    d = new String(kv.getValue());
                } else if (new String(kv.getQualifier()).equals("status")) {
                    e = new String(kv.getValue());
                }

                if (id.equals(a))
                {
                    f=new String(kv.getRow());
                    if(status.equals("1")) status="Y";
                    if (status.equals("2")) status="N";
                    HbaseUtilU hbaseUtilU = new HbaseUtilU();
                    String getresult =  hbaseUtilU.Update(f,id,name,price,time,status);
                }
            }


//                data goodsmessage = new data();
//                goodsmessage.setGoodsid(a);
//                goodsmessage.setGoodsname(b);
//                goodsmessage.setGoodsprice(c);
//                goodsmessage.setGoodstime(d);
//                goodsmessage.setGoodsstatus(e);
//                dataList.add(goodsmessage);

            }
//
//           String  getid= data.getGoodsid();
//           String getname= data.getGoodsname();
//           String getprice= data.getGoodsprice();
//           String gettime= data.getGoodstime();
//           String getstatus= data.getGoodsstatus();

//        if(status.equals("1")) status="Y";
//        else status="N";

        //String name=session.getAttribute("name").toString();



//        System.out.println("当前用户"+name +"浏览学生信息...");

        // 添加
//        HbaseUtilU hbaseUtilU = new HbaseUtilU();
//        String getresult =  hbaseUtilU.Update(id,name,price,time,status);

        response.setContentType("text/html;charset=utf-8");
//        PrintWriter pw =  response.getWriter();
//        pw.write(result);'
//         request.setAttribute("goodmessage",dataList);
        request.getRequestDispatcher("/freshdata").forward(request,response);




//        response.setContentType("text/html;charset=utf-8");
//
//        PrintWriter printWriter=response.getWriter();

    }
}
